#include "stdafx.h"
#include "FileNameUtils.h"
#include "Utils.h"

const LPCTSTR cJPEGExtensions[] = { _T("jpg"), _T("jpeg"), _T("jpe"), _T("jfif") };
const size_t cJPEGExtensionsCount = 4;

const LPCTSTR cPNGExtensions[] = { _T("png") };
const size_t cPNGExtensionsCount = 1;

const LPCTSTR cGIFExtensions[] = { _T("gif") };
const size_t cGIFExtensionsCount = 1;

const LPCTSTR cBMPExtensions[] = { _T("bmp") };
const size_t cBMPExtensionsCount = 1;

const LPCTSTR  cWordExtensions[] = { _T("doc"), _T("docx"), _T("rtf") };
const size_t cWordExtensionsCount = 3;


void GetDefaultDocExt(tstring& Ext)
{
	Ext = cWordExtensions[0];
}

void GetDefaultJPEGExt(tstring& Ext)
{
	Ext = cJPEGExtensions[0];
}

void GetDefaultPNGExt(tstring& Ext)
{
	Ext = cPNGExtensions[0];
}

void GetDefaultGIFExt(tstring& Ext)
{
	Ext = cGIFExtensions[0];
}

void GetDefaultBMPExt(tstring& Ext)
{
	Ext = cBMPExtensions[0];
}

bool HasDocExt(const tstring& PathToFile)
{
	return CheckFileExtension(PathToFile, cWordExtensions, cWordExtensionsCount);
}

bool HasJPEGExt(const tstring& PathToFile)
{
	return CheckFileExtension(PathToFile, cJPEGExtensions, cJPEGExtensionsCount);
}

bool HasPNGExt(const tstring& PathToFile)
{
	return CheckFileExtension(PathToFile, cPNGExtensions, cPNGExtensionsCount);
}

bool HasGIFExt(const tstring& PathToFile)
{
	return CheckFileExtension(PathToFile, cGIFExtensions, cGIFExtensionsCount);
}

bool HasBMPExt(const tstring& PathToFile)
{
	return CheckFileExtension(PathToFile, cBMPExtensions, cBMPExtensionsCount);
}

void GetDefaultImageExt(ImageType_t ImageType, tstring& Ext)
{
	switch( ImageType )
	{
	case imtJPEG: GetDefaultJPEGExt(Ext);
		break;
	case imtPNG: GetDefaultPNGExt(Ext);
		break;
	case imtGIF: GetDefaultGIFExt(Ext);
		break;
	case imtBMP: GetDefaultBMPExt(Ext);
		break;
	default:
		_ASSERT(FALSE);
	}
}

bool CheckImageFileExtension(ImageType_t ImageType, const tstring& FileName)
{
	switch( ImageType )
	{
	case imtJPEG: return HasJPEGExt(FileName);
	case imtPNG: return HasPNGExt(FileName);
	case imtGIF: return HasGIFExt(FileName);
	case imtBMP: return HasBMPExt(FileName);
	case imtUnknown: return false;
	default:
		_ASSERT(FALSE);
	}
	return false;
}

ImageType_t FileName2ImageType(const tstring& FileName)
{
	if( HasJPEGExt(FileName) )
		return imtJPEG;
	if( HasPNGExt(FileName) )
		return imtPNG;
	if( HasGIFExt(FileName) )
		return imtGIF;
	if( HasBMPExt(FileName) )
		return imtBMP;
	return imtUnknown;
}

bool IsImageExt(const tstring& Ext)
{
	if( Ext.size() <= 1 )
		return false;
	tstring ExtToCheck;
	if( *Ext.begin() != _T('.') ) //!!!!!!!!!
	{
		ExtToCheck = _T('.');
		ExtToCheck += Ext;
	}
	else
	{
		ExtToCheck = Ext;
	}
	return FileName2ImageType(ExtToCheck) != imtUnknown;
}

bool IsDocExt(const tstring& Ext)
{
	if( Ext.size() <= 1 )
		return false;
	tstring ExtToCheck;
	if( *Ext.begin() != _T('.') ) //!!!!!!!!!
	{
		ExtToCheck = _T('.');
		ExtToCheck += Ext;
	}
	else
	{
		ExtToCheck = Ext;
	}
	return HasDocExt(ExtToCheck);
}